package com.yinhe.music.yhmusic.localmusic.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusListObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseActivity;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.dialog.collect.CollectionDialog;
import com.yinhe.music.yhmusic.localmusic.adapter.LocalManagerAdapter;
import com.yinhe.music.yhmusic.login.LoginRegisterActivity;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.utils.DataHolder;
import com.yinhe.music.yhmusic.utils.MusicUtils;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 */
public class LocalManageActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.play)
    TextView play;
    @BindView(R.id.add_to)
    TextView addTo;
    @BindView(R.id.down)
    TextView down;
    @BindView(R.id.delete)
    TextView delete;
    @BindView(R.id.bottom_menu)
    FrameLayout bottomMenu;
    @BindView(R.id.toolbar_layout)
    LinearLayout llToolbarLayout;
    private boolean isSelect = false;
    private List<Music> mList = new ArrayList<>();
    private LocalManagerAdapter managerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage);
        ButterKnife.bind(this);
        RxBus.get().register(this);
        mList = DataHolder.getInstance().getData();
        llToolbarLayout.setVisibility(View.VISIBLE);
        setToolbar("批量操作");
        setBottom();
        setList();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    private void setList() {
        managerAdapter = new LocalManagerAdapter(mList);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this));
        recyclerView.setAdapter(managerAdapter);
        recyclerView.setHasFixedSize(true);
    }

    private void setBottom() {
        down.setVisibility(View.GONE);
        play.setOnClickListener(this);
        addTo.setOnClickListener(this);
        delete.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_select, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle item selection
        if (item.getItemId() == R.id.menu_all) {
            if (isSelect) {
                isSelect = false;
                item.setTitle("全选");
                updateSelect(false);
            } else {
                item.setTitle("取消全选");
                isSelect = true;
                updateSelect(true);
            }
        }
        return super.onOptionsItemSelected(item);
    }

    private void updateSelect(boolean select) {
        for (Music i : mList) {
            i.setChecked(select);
        }
        managerAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_to:
                addToMenu();
                break;
            case R.id.delete:
                deleteMusic();
                break;
            case R.id.play:
                addToPlayList();
                break;
            default:
                break;
        }
    }

    private void addToMenu() {
        if (Preferences.getLoginSn().isEmpty()) {
            startActivity(new Intent(this, LoginRegisterActivity.class));
            return;
        }
        List<Music> musicList = getSelectList();
        ArrayList<Integer> idList = new ArrayList<>();
        for (Music music : musicList) {
            idList.add((int) music.getSongId());
        }
        if (idList.isEmpty()) {
            Toast.makeText(this, "请选择要添加的歌曲", Toast.LENGTH_SHORT).show();
            return;
        }
        CollectionDialog dialog = CollectionDialog.newInstance(idList, IConstants.LOCAL);
        dialog.show(getSupportFragmentManager(), "CollectionDialog");
    }

    private void addToPlayList() {
        List<Music> musicList = getSelectList();
        if (mService != null) {
            mService.addMusicListToPlayList(musicList, mService.getPlayingPosition());
        }
    }

    private List<Music> getSelectList() {
        List<Music> musicList = new ArrayList<>();
        for (Music i : mList) {
            if (i.isChecked()) {
                musicList.add(i);
            }
        }
        return musicList;
    }

    private void deleteMusic() {
        MusicUtils.deleteLocalMusic(this, getSelectList());
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {@Tag(RxBusEventType.Local.DELETE_LOCAL_MUSIC)})
    public void onRxbusDeleteLocalMusic(RxbusListObject rxbusListObject) {
        List<Music> deleteList = (List<Music>) rxbusListObject.rxList;
        mList.removeAll(deleteList);
        managerAdapter.notifyDataSetChanged();
    }
}
